<template>
  <div class="app-container">
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <label>分类</label>
          <el-select clearable style="margin-left: 10px" v-model="selectedValue" placeholder="请选择产品分类"
                     @focus="typeDialogList"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <label style="margin-left: 20px">产品名称</label>
          <el-select clearable style="margin-left: 10px" v-model="selectedProductValue" placeholder="请选择产品"
                     @focus="productVoList"
          >

            <el-option
              v-for="item in productList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <label style="margin-left: 20px">规格</label>
          <el-select clearable style="margin-left: 10px" v-model="selectedProductSpecValue" placeholder="请选择产品规格"
                     @focus="productSpecVoList"
          >
            <el-option
              v-for="item in productSpecList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <label style="margin-left: 20px">标签代表数量</label>
          <el-input v-model="labelNumber" style="width: 120px;margin-left: 10px"
                    placeholder="请输入产品单个标签代表数量" clearable
          />
          <label style="margin-left: 20px">产品数量</label>
          <el-input v-model="labelProductNumber" label="产品数量" style="width: 120px;margin-left: 10px"
                    placeholder="请输入产品标签数量" clearable
          />
          <label style="margin-left: 20px">标签数量：</label>
          <label>
            <template>{{ labelProductNumber / labelNumber }}</template>
          </label>
        </el-row>
        <el-row style="display:flex;margin-top: 20px">
          <el-button type="primary" size="mini" @click="save()">生成数据</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
    >

      <el-table-column
        label="序号"
        width="70"
        align="center"
      >
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="orderNo" label="记录单号" width="180"/>
      <el-table-column prop="number" label="生成标签数量" width="110"/>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column prop="updateTime" label="更新时间"/>

      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="download(scope.row.id)" title="下载">
            下载
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)"
                     title="删除"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />


  </div>
</template>
<script>
import api from '@/api/label/labelRecord'
import typeApi from '@/api/basic/productType'
import productApi from '@/api/basic/product'
import specApi from '@/api/basic/productSpec'
import useDownload from '@/utils/download'

export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      listLoading: false, // 数据是否正在加载
      list: null, // 列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 3, // 每页记录数
      searchObj: {}, // 查询表单对象
      selectedValue: this.value,
      selectedProductValue: this.value,
      selectedProductSpecValue: this.value,
      createTimes: [],
      typeList: [{
        id: 0,
        name: ''
      }],
      productList: [{
        id: 0,
        name: ''
      }],
      productSpecList: [{
        id: 0,
        name: ''
      }],
      labelNumber: 0,
      labelProductNumber: 0,
      dialogVisible: false,
      sysUser: {},

      dialogRoleVisible: false,
      allRoles: [], // 所有角色列表
      userRoleIds: [], // 用户的角色ID的列表
      isIndeterminate: false, // 是否是不确定的
      checkAll: false // 是否全选
    }
  },
  created() {
    //调用列表方法
    this.fetchData()
  },
  watch: {
    value(newValue) {
      this.selectedValue = newValue
    },
    productValue(newValue) {
      this.selectedValue = newValue
    },
    specialValue(newValue) {
      this.selectedValue = newValue
    }
  },
  methods: {
    typeDialogList() {
      this.selectedProductValue = ''
      this.selectedProductSpecValue = ''
      typeApi.getList()
        .then(response => {
          this.typeList = response.data
        })
    },
    productVoList() {
      if (!this.selectedValue) {
        return
      }
      this.selectedProductSpecValue = ''
      productApi.getListByTypeId(parseInt(this.selectedValue, 10))
        .then(response => {
          this.productList = response.data
        })
    },
    productSpecVoList() {
      console.log(this.selectedProductValue)
      if (!this.selectedProductValue) {
        return
      }
      specApi.getPageList({ productId: parseInt(this.selectedProductValue, 10) })
        .then(response => {
          this.productSpecList = response.data
        })
    },
    //删除
    removeDataById(id) {
      this.$confirm('此操作将永久删除生成的标签记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //调用方法删除
        api.removeById(id)
          .then(response => {
            //提示
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            //刷新
            this.fetchData()
          })
      })
    },
    //添加
    save() {
      this.sysUser.productSpecId = this.selectedProductSpecValue
      this.sysUser.productNum = this.labelProductNumber
      this.sysUser.labelProductNum = this.labelNumber
      api.save(this.sysUser)
        .then(response => {
          //提示
          this.$message.success('操作成功')
          //刷新
          this.fetchData()
        })
    },
    download(id) {
      useDownload(
        '/admin/factory-product-label/download/'+id,
        `epc_${new Date().getTime()}.xlsx`
      )
    },
    //列表
    fetchData(page = 1) {
      this.page = page
      if (this.createTimes && this.createTimes.length == 2) {
        this.searchObj.createTimeBegin = this.createTimes[0]
        this.searchObj.createTimeEnd = this.createTimes[1]
      }
      api.getPageList(this.page, this.limit)
        .then(response => {
          this.list = response.data.records
          this.total = response.data.total
        })
    }
  }
}

</script>
